var menumap_speedScale = 0.5;
var menumap_margin = 2;

function menumap_getcontainer(menumap_instanceid)
{
	var menumap_instance = document.getElementById(menumap_instanceid);
	if(menumap_instance) {
		for(var i = 0; i < menumap_instance.childNodes.length; i++) {
			if("menumap_container" == menumap_instance.childNodes[i].id)
				return menumap_instance.childNodes[i];
		}
	}
	return null;
}

function menumap_getchildbyID(menumap_element, element_ID)
{
	if(menumap_element) {
		for(var i = 0; i < menumap_element.childNodes.length; i++) {
			if(element_ID == menumap_element.childNodes[i].id)
				return menumap_element.childNodes[i];
		}
	}
	return null;
}

function menumap_opacity(menumap_instanceid, opacity) { 
	
	if(opacity < 0)
		opacity = 0;
	else if(opacity > 100)
		opacity = 100;
	
    var container_style = menumap_getcontainer(menumap_instanceid).style; 
    container_style.opacity = (opacity / 100); 
    container_style.MozOpacity = (opacity / 100); 
    container_style.KhtmlOpacity = (opacity / 100); 
    container_style.filter = "alpha(opacity=" + opacity + ")"; 
}

function menumap_openUpdate(menumap_instanceid)
{

	var menumap_container = menumap_getcontainer(menumap_instanceid);	
	if(menumap_container) {
		if(parseInt(menumap_container.style.left) < menumap_margin) {
		
			var leftInt = parseInt(menumap_container.style.left);
			var leftInc = ((menumap_margin + leftInt) - (0.5 * leftInt * menumap_speedScale));
			menumap_container.style.left = (leftInc > menumap_margin ? menumap_margin : leftInc) + "px";
			menumap_opacity(menumap_instanceid, leftInc + 90);
		}
		else 
			return;

		setTimeout('menumap_openUpdate(\'' + menumap_instanceid + '\')', 15);
	}
}

function menumap_closeUpdate(menumap_instanceid)
{
	var menumap_container = menumap_getcontainer(menumap_instanceid);	
	if(menumap_container) {
		if(parseInt(menumap_container.style.left) + parseInt(menumap_container.style.width) > menumap_margin) {
		
//			alert(menumap_container.style.left + "," + leftInt + "," + leftInc);
			
			//(leftInc > menumap_margin ? leftInc : 0) 
//			alert(menumap_container.style.left);
			menumap_container.style.left = (parseInt(menumap_container.style.left) - parseInt(menumap_container.style.width) * 0.15) + "px";
	//		alert(menumap_container.style.left);
			menumap_opacity(menumap_instanceid, 100);
		}
		else 
			return;

		setTimeout('menumap_closeUpdate(\'' + menumap_instanceid + '\')', 15);
	}
}

function menumap_toggleDisplay()
{
	
	var menumap_div = this._divActivator;
	var activator_this = document.getElementById(menumap_div.id);
	var menumap_instanceid = (activator_this.id) + "_menumap_div";
	var menumap_instance = document.getElementById(menumap_instanceid);
	
	if(this._isOpen == false) {
	
		menumap_instance = document.createElement("div");
		menumap_instance.innerHTML = document.getElementById("menumap_template").innerHTML;
		menumap_instance.id = menumap_instanceid;
		activator_this.insertBefore(menumap_instance, activator_this.firstChild);
		
		var menumap_container = menumap_getcontainer(menumap_instanceid);
		var menumap_instance_container_style = menumap_getcontainer(menumap_instanceid).style; 
		var menumap_instance_style = menumap_instance.style;
		var activator_this_style = activator_this.style;
		
		var x_pos = (activator_this_style.left ? parseInt(activator_this_style.left) : 0) + (activator_this_style.width ? parseInt(activator_this_style.width) : 0);
		var y_pos = (activator_this_style.top ? parseInt(activator_this_style.top) : 0) + (activator_this_style.height ? parseInt(activator_this_style.height) : 0);
	 
		menumap_instance_style.overflow = "hidden";
		menumap_instance_style.visibility = "visible";
		menumap_instance_style.left = x_pos + "px";
		menumap_instance_style.top = y_pos + "px";	
		menumap_instance_style.position = "absolute";
		menumap_instance_style.width = menumap_instance_container_style.width;
		menumap_instance_style.height = menumap_instance_container_style.height;
		menumap_instance_style.zIndex = 1;
		
	
		menumap_instance_container_style.left = -parseInt(menumap_instance_style.width) + "px";

			this._isOpen = true;
		setTimeout('menumap_openUpdate(\'' + menumap_instanceid + '\')', 33);
	}
	else {
					this._isOpen = false;
		setTimeout('menumap_closeUpdate(\'' + menumap_instanceid + '\')', 33);
		
	}
	
}

function menumap(divActivator)
{
	this.toggleDisplay = menumap_toggleDisplay;
	this._divActivator = divActivator;
	this._isOpen = false;
}
